<template>
  <ele-modal
    form
    width="90%"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        v-model:selections="selections"
        :show-overflow-tooltip="true"
        highlight-current-row
        stripe
        :toolbar="false"
        :pagination="false"
        border
      >
        <template #belongs="{ row }">
          {{ row.belongs }} {{ row.belongsName }}
        </template>
        <template #img="{ row }">
          <span
            v-if="row.images !== ''"
            class="pointer blue"
            @click="openImages(row)"
            >查看</span
          >
          <span v-else>无</span>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 图片 -->
    <images v-model="showImages" :data="current" />
  </ele-modal>
</template>

<script setup>
  import { ref, computed, watch } from 'vue';
  import { pageCompanyIncomeExpenssReportType } from '@/api/company';
  import Images from '@/views/insurance/components/images.vue';
  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,

    data: Object
  });
  const emit = defineEmits(['done', 'update:modelValue']);
  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };
  /** 表格实例 */
  const tableRef = ref(null);
  /** 表格选中数据 */
  const selections = ref([]);
  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        width: 80,
        prop: 'id',
        label: '编号'
      },
      {
        width: 80,
        prop: 'io_type',
        label: '类型'
      },
      {
        align: 'center',
        slot: 'belongs',
        label: '费用归属'
      },
      {
        width: 100,
        prop: 'cate',
        label: '费用分类'
      },
      {
        width: 200,
        prop: 'remark',
        label: '费用说明'
      },
      {
        width: 100,
        slot: 'img',
        label: '费用凭证',
        align: 'center'
      },
      {
        width: 110,
        prop: 'amount',
        label: '金额'
      },
      {
        width: 120,
        prop: 'area_share',
        label: '区域承担'
      },
      {
        width: 120,
        prop: 'company_share',
        label: '车队承担'
      }
    );

    return cols;
  });

  /** 表格数据源 */
  const datasource = async ({ page, where, orders }) => {
    const result = await pageCompanyIncomeExpenssReportType({
      ...where,
      ...orders,
      page,
      limit: 9999,
      belongs_month: props.data.belongs_month,
      area_id: props.data.area_id,
      company_id: props.data.company_id,
      cate_id: props.data.cate_id
    });
    return result;
  };
  const current = ref([]);

  /** 查看图片 */
  const showImages = ref(false);
  const openImages = (data) => {
    current.value = data;
    showImages.value = true;
  };

  const title = ref('');
  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        const data = props.data;
        title.value = data.belongs_month + '的' + data.cateTitle + '的费用列表';
        console.log('打开');
      } else {
        console.log('关闭了');
      }
    }
  );
</script>
